#ifndef HEADER_H
#define HEADER_H

//#include <math.h>
//#include <stdlib.h>
//#include <time.h>

/* SINK address */
#define SINK_ADDR 0

/* The time interval for the unit of local clock */
#define ONE_SENCOND 1000

/* The upper of mobile nodes*/
#define MAX_NODES	16

/* The time cycle for Topology Detection */
#define TOPO_TIMER_INTERVAL	20000

/* The time inerval after Topology Detection begins*/
#define TRANSFER_INTERVAL 10000

#define CONTROLMOTE	17

enum
{
	/* Port number for CMD(TimeSync/LogRequest) messages */
	CMDPORT = 20,

	/* Port number for Topology Detection messages */
	TOPOPORT = 21,

	/* Port number for KeyFragment messages */
	DATAPORT = 22,

	/* Port number for Log messages */
	LOGPORT = 23
};

/* BUffer of mobile nodes*/
#define BUFFER 	4

#define MAX_LOG_BUFFER	50

/* The type of seconds*/
typedef uint32_t uclock_t;

/*	Set Structure for storing neighbours.
 *	Each 2 bits which are set by 11 presents one neighbour.
 *	The total number of neighbours can not be more than 15.
 */
typedef uint32_t neighbour_t;

/*	Set Structure for storing key segments.
 *	Each bit which is set by 1 presents one fregment.
 *	Because each key is fragmented by 2 and the maxium count of nodes is 16
 *	So the total number of neighbours can not be more than 32.
 */
typedef uint32_t keys_t;

typedef uint16_t addr16_t;

/* The state of one mobile node */
typedef enum state
{
	INFECTED = 1,	//No key
	UNI = 2,		//Its own keys
	FORWARD = 4		//Others's keys
}Type;

/* The type of a message */
typedef enum msgType
{
	SYNC = 1,
	TOPO = 2,
	KEYS = 3,
	LOG_REQUEST = 4
}MsgType;

/* Message for test */
typedef struct
{
	uint8_t val;
}TestMsg;

/* Message for communication */
typedef struct
{
	MsgType type;
	addr16_t src;
	addr16_t dst;
	keys_t keys;
}KUMsg;

typedef struct MsgNode
{
	KUMsg node;
	struct MsgNode *next;
}MsgNode;

typedef enum
{  
	ENCOUNTER = 1,
	EVENT_UNI = 2,
	EVENT_FWD = 4
}LogType;

typedef struct
{
	LogType lType;
	uclock_t lTime;
	addr16_t lSrc;
	addr16_t lDst;
	uint32_t data;
}LogMsg;

#endif
